package com.lightsaber.trade.as.service.email;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lightsaber.trade.as.dao.email.EmailSettingDao;
import com.lightsaber.trade.as.entity.email.EmailNotificationType;
import com.lightsaber.trade.as.entity.email.EmailSetting;
import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.service.BaseServiceImpl;

/**
 * @author
 * 
 */
@Service
@Transactional
public class EmailSettingManager extends BaseServiceImpl<EmailSetting, String> {
    @Autowired
    private EmailSettingDao emailSettingDao;

    @Override
    protected BaseDao<EmailSetting, String> getDao() {
        // TODO Auto-generated method stub
        return emailSettingDao;
    }

    /**
     * according to email notification type find roles
     */
    public List<EmailSetting> findByEmailNotificationType(
            final EmailNotificationType emailNotificationType) {
        return emailSettingDao.findBy("emailNotificationType", emailNotificationType);
    }

    public void deleteByEmailNotificationType(final EmailNotificationType emailNotificationType) {
        emailSettingDao.deleteByEmailNotificationType(emailNotificationType);
    }

    public void saveBySelectRoles(final EmailNotificationType emailNotificationType,
            final List<String> selectRoleCodes) {
        // 1. delete all roles by email notification type
        deleteByEmailNotificationType(emailNotificationType);
        // 2. save by select role codes
        if (selectRoleCodes != null) {
            for (String roleCode : selectRoleCodes) {
                EmailSetting emailSetting = new EmailSetting();
                emailSetting.setEmailNotificationType(emailNotificationType);
                emailSetting.setRoleCode(roleCode);
                emailSettingDao.save(emailSetting);
            }
        }
    }
}
